package com.zzl.finance.data.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.finance.data.dto.BankAccountAddDto;
import com.zzl.finance.data.dto.BankAccountUpdateDto;
import com.zzl.finance.data.entity.BankAccountEntity;
import com.zzl.finance.data.service.BankAccountService;
import com.zzl.general.aly.pojo.BankApRecord;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import com.zzl.sharding.user.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 银行账户管理
 *
 * @author hyt
 * @date 2021-11-30 19:12:59
 */
@ApidocController("2银行账户管理")
@RestController
@SuppressWarnings("javadoc")
@RequestMapping("data/bankAccount")
@Slf4j
public class BankAccountController extends WebBaseCtrl {

    @Autowired
    private BankAccountService bankAccountService;

    @GetMapping("/getBankAccount")
    @ApidocOperation(description = "打款认证-企业打款认证查询", operationId = "data/getBankAccount")
    @ApidocReturnR
    public Result getBankAccount() {
        return Result.ok().data(bankAccountService.getBankAccount(this.getUser()));
    }

    @GetMapping("/getAccountList")
    @ApidocOperation(description = "1银行账户下拉框", operationId = "dataEnterRelation2/getAccountList")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "user", valueClass = UserEntity.class)
    })
    public Result getAccountList() {
        return Result.ok().put("list", bankAccountService.getAccountList(this.getUser()));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApidocOperation(description = "保存", operationId = "bankAccountSave2")
    @ApidocReturnR
    public Result save(@ApidocParam(name = "保存的对象值", required = true) @Validated @RequestBody BankAccountAddDto accountAddDto) {
        try {
            bankAccountService.saveBankAccount(accountAddDto, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 分页查询银行账户数据
     */
    @GetMapping("/listPage")
    @ApidocOperation(description = "分页查询银行账户数据", operationId = "dataEnterRelation2")
    @ApidocReturnPage(genericTypeClass = BankAccountEntity.class)
    public Result listPage(@ApidocParamsPage(name = "params", keyName = {"enterpriseId", "accountOpeningOutlet", "corporateAccount"}, keyDescription = {"企业id", "开户网点", "银行账号"})
                           @RequestParam Map<String, Object> params) {
        PageUtils page = bankAccountService.listWhitePage(params, this.getUser());
        return Result.ok().put("page", page);
    }

    /**
     * 修改的企业的评级
     */
    @PutMapping("/updateBankAccount")
    @ApidocOperation(description = "修改银行账户", operationId = "dataEnterRelation3")
    @ApidocReturnR
    public Result update(@ApidocParam(name = "修改的企业的评级", required = true) @RequestBody BankAccountUpdateDto updateDto) {
        try {
            bankAccountService.updateBankAccount(updateDto, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApidocOperation(description = "删除银行账户", operationId = "dataEnterRelation4/1s")
    @ApidocReturnR
    public Result delete(@ApidocParam(name = "删除银行账户对象id集合", required = true) @RequestBody Integer[] ids) {
        bankAccountService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }


    /**
     * 获取当前企业id获取所有开户网点
     */
    @GetMapping("/getAccountOpeningOutletByLoginUser/{enterpriseId}")
    @ApidocOperation(description = "获取当前企业id获取所有开户网点String ", operationId = "dataEnterRelation5")
    @ApidocReturnPage(genericTypeClass = BankAccountEntity.class)
    public Result getAccountOpeningOutletByLoginUser(@ApidocParam(name = "enterpriseId", required = true) @PathVariable("enterpriseId") String enterpriseId) {
        List<String> bankAccount = bankAccountService.getAccountOpeningOutletByLoginUser(enterpriseId, this.getUser());
        return Result.ok().put("list", bankAccount);
    }

    /**
     * 获取当前企业id获取所有开户网点
     */
    @GetMapping("/noParamsGetAccountOpeningOutletByLoginUser")
    @ApidocOperation(description = "无参获取当前企业id获取所有开户网点", operationId = "noParamsGetAccountOpeningOutletByLoginUser")
    @ApidocReturnPage(genericTypeClass = BankAccountEntity.class)
    public Result noParamsGetAccountOpeningOutletByLoginUser() {
        List<String> bankAccount = bankAccountService.noParamsGetAccountOpeningOutletByLoginUser(this.getUser());
        return Result.ok().put("list", bankAccount);
    }

    /**
     * 企业id获取所有开户网点
     */
    @GetMapping("/listByEnterpriseId/{enterpriseId}")
    @ApidocOperation(description = "获取当前企业id获取所有开户网点listBean ", operationId = "/listByEnterpriseId/{enterpriseId}")
    @ApidocReturnPage(genericTypeClass = BankAccountEntity.class)
    public Result listByEnterpriseId(@ApidocParam(name = "enterpriseId", required = true) @PathVariable("enterpriseId") String enterpriseId) {
        List<BankAccountEntity> bankAccount = bankAccountService.listByEnterpriseId(enterpriseId, this.getUser());
        return Result.ok().put("list", bankAccount);
    }

    /**
     * 获取银行开户网点
     */
    @GetMapping("/getAllAccountOpeningOutlet")
    @ApidocOperation(description = "获取获取所有开户网点 ", operationId = "getAllAccountOpeningOutlet/a1")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "list", valueClass = BankApRecord.class)
    })
    public Result getAllAccountOpeningOutlet(@ApidocParamsPage(name = "params", keyName = {"bank", "province", "city"}, keyDescription = {"开户行全称", "开户行所在省", "开户行所在市"})
                                             @RequestParam Map<String, Object> params) {
        try {
            List<BankApRecord> bankAccount = bankAccountService.getAllAccountOpeningOutlet(params, this.getUser());
            return Result.ok().list(bankAccount);
        } catch (SysException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 检测开户网点是否正确
     */
    @GetMapping("/getLNameIsCorrect")
    @ApidocOperation(description = "检测开户网点是否正确", operationId = "getAllAccountOpeningOutlet")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_ARRAY, key = "list", valueClass = BankApRecord.class)
    })
    public Result getLNameIsCorrect(@ApidocParamsPage(name = "params", keyName = {"lName"}, keyDescription = {"开户行网点"})
                                    @RequestParam Map<String, Object> params) {
        try {
            bankAccountService.getLNameIsCorrect(params, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMessage());
        }
    }

}
